package com.my.screenadapter.sql;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;


import com.google.gson.Gson;
import com.my.screenadapter.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 测试sqlite
 */

public class SqlAddstrActivity extends AppCompatActivity {

   public static String  dbFile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sql_addstr);
       findViewById(R.id.initSql).setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               scanVirus();
           }
       });
        dbFile=getExternalFilesDir("MySql")+ File.separator+"grassland.db";
        findViewById(R.id.initJson).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initJsonTxt();
            }
        });
    }

    private void initJsonTxt() {
        int count=0;
        long start = System.currentTimeMillis();
        Log.i("====",start+"");
        try {
            InputStreamReader inputStream =new InputStreamReader(getAssets().open("regions.txt"),"UTF-8") ;
            BufferedReader bufferedReader=new BufferedReader(inputStream);
            String line=null;
            StringBuilder builder=new StringBuilder();
            while ((line=bufferedReader.readLine())!=null){
                builder.append(line);
            }
            bufferedReader.close();
            inputStream.close();
            Gson gson = new Gson();
            AddstrModel addstrModel = gson.fromJson(builder.toString(), AddstrModel.class);

            if (addstrModel!=null){
                List<AddstrModel.ProvinceBean> province = addstrModel.getProvince();

                Log.i("province====",province.size()+"---");
//                for (int i = 0; i < province.size()&&i<5; i++) {
                for (int i = 0; i < province.size(); i++) {
                    AddstrModel.ProvinceBean provinceBean = province.get(i);
                    Log.i("province====",provinceBean.getName()+"---"+provinceBean.getRegionId());
                    List<AddstrModel.ProvinceBean.CityBean> city = province.get(i).getCity();
                    Log.i("city====",city.size()+"");
//                    for (int j = 0; j < city.size() && j < 5; j++) {
                    for (int j = 0; j < city.size() ; j++) {
                        AddstrModel.ProvinceBean.CityBean cityBean = city.get(j);
                        Log.i("city====",cityBean.getName()+"---"+cityBean.getRegionId());
                        List<AddstrModel.ProvinceBean.CityBean.CountyBean> county = cityBean.getCounty();
                        Log.i("county====",county.size()+"");
//                        for (int k = 0; k < county.size() &&k<5; k++) {
                        for (int k = 0; k < county.size() ; k++) {
                            AddstrModel.ProvinceBean.CityBean.CountyBean countyBean = county.get(i);
                            Log.i("county====",countyBean.getName()+"---"+countyBean.getRegionId());
                            List<AddstrModel.ProvinceBean.CityBean.CountyBean.TownBean> town = countyBean.getTown();
                            Log.i("town====",town.size()+"");
//                            for (int l = 0; l < town.size()&&l<5; l++) {
                            for (int l = 0; l < town.size(); l++) {
                                AddstrModel.ProvinceBean.CityBean.CountyBean.TownBean townBean = town.get(l);
                                Log.i("town====",townBean.getName()+"---"+townBean.getRegionId());
                                List<AddstrModel.ProvinceBean.CityBean.CountyBean.TownBean.VillageBean> village = townBean.getVillage();
                                Log.i("village====",village.size()+"");
//                                for (int m = 0; m < village.size()&&m<5; m++) {
                                for (int m = 0; m < village.size(); m++) {
                                    AddstrModel.ProvinceBean.CityBean.CountyBean.TownBean.VillageBean villageBean = village.get(m);
                                    Log.i("village====",villageBean.getName()+"---"+villageBean.getRegionId());
                                    count++;
                                }
                            }
                        }
                    }
                }
            }
            Log.i("count====",count+"");
            Log.i("endtime===",System.currentTimeMillis()-start+"");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void scanVirus() {
        new Thread() {
            public void run() {
                InputStream is = null;//欲导入的数据库
                try {
                    is = getAssets().open("grassland.db");
                    FileOutputStream fos = new FileOutputStream(dbFile);
                    byte[] buffer = new byte[1024];
                    int count = 0;
                    while ((count = is.read(buffer)) > 0) {
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //   ȡ
                PackageManager pm = getPackageManager();
                //   ȡ     ԰ װ İ    Ϣ
                List<PackageInfo> packageInfo = pm.getInstalledPackages(0);
                //    ý         ֵ
//                pb_scan_status.setMax(packageInfo.size());
                int progress = 0;
                //
                for (PackageInfo info : packageInfo) {
                    //  õ    ·
                    String path = info.applicationInfo.sourceDir;
                    //    ø   ļ ·  MD5   ܵ     ժҪ
//                    String md5 = getFileMD5(path);
//                    System.out.println(md5 + "----------------");
                    //   ȡ  ݿ
                    SQLiteDatabase db = SQLiteDatabase.openDatabase(
                           dbFile,
                            null, SQLiteDatabase.OPEN_READONLY);
                    //   ѯ
                    Cursor cursor = db.query("ningxiaregion", null, null, null, null, null, null);
//                    Cursor cursor = db.rawQuery(
//                            "select desc from ningxiaregion where ProvinceId=?",
//                            new String[] { "64" });
                    if (cursor.moveToNext()) {
                        String regionid = cursor.getString(2);
//                        cursor.getStringAndShow()

                      Log.i("db===",regionid + "=================");
//                        AddstrRegion addstrRegion = new AddstrRegion();
//                        addstrRegion.setProvince();
//                        Message msg = Message.obtain();
//                        msg.what = FIND;
//                        msg.obj = info;
//                        handler.sendMessage(msg);
                    } else {
//                        Message msg = Message.obtain();
//                        msg.what = NOT_FIND;
//                        msg.obj = info;
//                        handler.sendMessage(msg);
                    }
                    progress++;
//                    SystemClock.sleep(50);
//                    pb_scan_status.setProgress(progress);
                }
                // ѭ
//                Message msg = Message.obtain();
//                msg.what = FINISH;
//                handler.sendMessage(msg);

            };
        }.start();

    }
}
